using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace SubSonic
{
    public class Select : SqlQuery
    {
        public Select(DataProvider provider, params string[] columns)
        {
            this.provider = provider;
            SelectColumnList = columns;
            SQLCommand = SqlFragment.SELECT;
            SqlQuery result = new SqlQuery(provider);
        }

        public Select(params Aggregate[] aggregates)
        {
            foreach(Aggregate agg in aggregates)
                Aggregates.Add(agg);
        }
        public Select(DataProvider provider, params Aggregate[] aggregates) {
            this.provider = provider;
            foreach (Aggregate agg in aggregates)
                Aggregates.Add(agg);
        }

        public Select(params string[] columns)
        {
            SQLCommand = SqlFragment.SELECT;
            if(columns.Length == 1 && columns[0].Contains(","))
            {
                //user entered a single string column list: "col1, col2, col3"
                SelectColumnList = columns[0].Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                //user entered an array
                SelectColumnList = columns;
            }
            SqlQuery result = new SqlQuery();
        }

        public Select()
        {
            SQLCommand = SqlFragment.SELECT;
        }

        public Select(TableSchema.Table tbl)
        {
            SQLCommand = SqlFragment.SELECT;
            provider = tbl.Provider;
            FromTables.Add(tbl);
        }

        public Select(DataProvider provider)
        {
            this.provider = provider;
            SQLCommand = SqlFragment.SELECT;
        }

        public SqlQuery Expression(string sqlExpression)
        {
            Expressions.Add(sqlExpression);
            return this;
        }

        public SqlQuery Top(string top)
        {
            if(!top.ToLower().Trim().Contains("top"))
                top = " TOP " + top;
            TopSpec = top;
            return this;
        }
    }
}